import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';
import { WeaInputLocale, WeaLocaleProvider, WeaDialog } from 'ecCom';

const getCurrentLabel = WeaInputLocale.getCurrentLabel;// 过滤多语言字符串
const getLabel = WeaLocaleProvider.getLabel;// 获取多语言

export default class ApiSingleShowStore {
    @observable apiId = '';// 接口id
    @observable baseList;// 基本信息
    @observable isSuccess = false;// 是否成功数据
    @observable apiName = '';// 接口名称
    @observable updata_date = '';// 数据更新时间
    @observable msgcode = '';// 返回信息
    @observable loading = true;// 加载状态
    @observable apiJson = '';
    @observable params = {};
    @observable json = {}

    @action
    init = (comBj) => {
      if (comBj.name) {
        this.apiName = comBj.name;
      }

      if (comBj.showAlertIcon) {
        this.showAlertIcon = comBj.showAlertIcon;
      }

      if (comBj.params.apiId) {
        this.apiId = comBj.params.apiId;
      }

      this.params = comBj.params;
      this.callApi();
    }

    // 调用API
    @action
    callApi = () => {
      Apis.getApi({ ...this.params, isList: 1, skip: this.skip }).then((data) => {
        if (data.status == 'success') {
          this.json = JSON.parse(data.json);
          this.updata_date = `${data.updata_date} ${data.updata_time}`;// 最后更新时间
          this.isSuccess = true;
        } else {
          if (data.json && JSON.parse(data.json).status == 202) {
            setTimeout(() => { this.callApi(); }, 5000);
            return;
          }
          this.msgcode = data.msgcode;
          this.isSuccess = false;
        }
        this.loading = false;
      });
    }
}
